package com.lzz.bottomencapsulation.customview.selectimage.adapter;

import android.content.Context;
import android.view.ViewGroup;

import com.lzz.bottomencapsulation.adapter.MvpBaseRecyclerAdapter;
import com.lzz.bottomencapsulation.customview.selectimage.ImageConfig;
import com.lzz.bottomencapsulation.customview.selectimage.bean.Image;
import com.lzz.bottomencapsulation.customview.selectimage.viewholder.Item_camera;
import com.lzz.bottomencapsulation.customview.selectimage.viewholder.Item_image;
import com.lzz.bottomencapsulation.holder.MvpRVViewHolder;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;

/**
 * 图片适配器
 * Created by Yancy on 2015/12/2.
 */
public class ImageAdapter extends MvpBaseRecyclerAdapter<MvpRVViewHolder> {


    private List<Image> imageList;

    private List<Image> selectedImageList = new ArrayList<>();

    private final static String TAG = "ImageAdapter";

    private static final int TYPE_CAMERA = 0;

    private static final int TYPE_NORMAL = 1;

    private boolean showCamera = true;//是否显示相机选项

    private boolean showSelectIndicator = true;

    private int mItemSize;

    private ImageConfig imageConfig;

    public ImageAdapter(Context context, List<Image> imageList, ImageConfig imageConfig, OnItemClickListener onItemClickListener) {
        super(context, onItemClickListener);
        this.imageList=imageList;
        this.imageConfig = imageConfig;
    }

    @NonNull
    @Override
    public MvpRVViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if (getItemViewType(i)==TYPE_CAMERA){
            return new Item_camera(getContext());
        }else {
            Item_image item_image= new Item_image(getContext(),showSelectIndicator,imageConfig);
            return item_image;
        }
    }

    @Override
    public int getItemCount() {
        return showCamera ? imageList.size() + 1 : imageList.size();
    }

    @Override
    public void onBindView(@NonNull MvpRVViewHolder viewHolder, int i) {
        if (getItemViewType(i)==TYPE_CAMERA){
            viewHolder.setViewData(getItem(i));
            viewHolder.initView();
        }else {
            ( (Item_image) viewHolder).setViewData(getItem(i));
            ( (Item_image) viewHolder).setSelectedImageList(selectedImageList);
            ( (Item_image) viewHolder).initView();
        }
    }

    @Override
    public void onBindView(MvpRVViewHolder holder, int position, List<Object> payloads) {
        if (payloads.isEmpty()){
            onBindViewHolder(holder,position);
        }else {
            if (getItemViewType(position)==TYPE_NORMAL){
                ( (Item_image) holder).setViewData(getItem(position));
                ( (Item_image) holder).setSelectedImageList(selectedImageList);
                ( (Item_image) holder).initView();
            }
        }
    }

    public void setDefaultSelected(ArrayList<String> resultList) {
        for (String filePath : resultList) {
            Image image = getImageByPath(filePath);
            if (image != null) {
                selectedImageList.add(image);
            }
        }
        if (selectedImageList.size() > 0) {
            notifyDataSetChanged();
        }
    }

    private Image getImageByPath(String filePath) {
        if (imageList != null && imageList.size() > 0) {
            for (Image image : imageList) {
                if (image.path.equalsIgnoreCase(filePath)) {
                    return image;
                }
            }
        }
        return null;
    }

    public Image getItem(int position) {
        if (showCamera) {
            if (position == 0) {
                return null;
            }
            return imageList.get(position - 1);
        } else {
            return imageList.get(position);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (showCamera && position == 0) {
            return TYPE_CAMERA;
        }
        return TYPE_NORMAL;
    }

    public void setShowSelectIndicator(boolean showSelectIndicator) {
        this.showSelectIndicator = showSelectIndicator;
    }

    public void setShowCamera(boolean showCamera) {
        if (this.showCamera == showCamera) return;
        this.showCamera = showCamera;
        notifyDataSetChanged();
    }

    public void select(Image image,int position) {

        if (selectedImageList.contains(image)) {
            selectedImageList.remove(image);
        } else {
            selectedImageList.add(image);
        }
        notifyItemChanged(position,"gengxin");

    }

    public boolean isShowCamera() {
        return showCamera;
    }

}